<?php

/**

 * @see Alinks_ImportExport_Export_Abstract

 */

require_once('Alinks/ImportExport/Export/Abstract.php');



/**

 * @see Alinks_Version

 */

require_once('Alinks/Version.php');



/**

 * Utility class for exporting the defined keyphrases as XML

 *

 * Copyright (C) 2008  Sean Hickey

 * 

 * This program is free software: you can redistribute it and/or modify

 * it under the terms of the GNU General Public License as published by

 * the Free Software Foundation, either version 3 of the License, or

 * (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *

 * @author Sean Hickey <sean@headzoo.com>

 * @version $Id: Xml.php 390 2008-07-03 05:40:51Z sean $

 * @package Alinks_ImportExport

 */

class Alinks_ImportExport_Export_Xml extends Alinks_ImportExport_Export_Abstract

{

	/**

	 * Returns the keyphrases as an XML string

	 *

	 * Returns the keyphrases (and optionally the settings and factory

	 * settings) as an XML string.

	 *

	 * @return string

	 */

	public function export()

	{

		$doc = new DOMDocument('1.0', 'UTF-8');

		$doc_type = 

			DOMImplementation::createDocumentType("Export", "alinks 2.0 Export", "http://headzoo.com/dtd/alinks_2_0.dtd");

		$doc = DOMImplementation::createDocument(null, null, $doc_type);



		$doc->appendChild(new DOMComment('Generated by aLinks v' . Alinks_Version::VERSION . ' on ' . date('r')));

		$root = $doc->appendChild(new DOMElement('Export'));

		$root->appendChild(new DOMAttr('xmlns', 'urn:headzoo-com:alinks_2_0'));

		$root->appendChild(new DOMAttr('alinks_version', Alinks_Version::VERSION));

		$settings = $root->appendChild(new DOMElement('Settings'));

		

		if ($this->_options[self::EXPORT_SETTINGS]) {

			$this->_exportSettings($settings);

		}

		if ($this->_options[self::EXPORT_FACTORY_SETTINGS]) {

			$this->_exportFactorySettings($settings);

		}

		$this->_exportKeyphrases($root);

		

		$doc->formatOutput = true;

		return $doc->saveXML();

	}

	

	/**

	 * Returns the MIME type for the exported document

	 *

	 * @return string

	 */

	public function getMimeType()

	{

		return 'text/xml';

	}

	

	/**

	 * Returns the file extension for the exported document

	 *

	 * @return string

	 */

	public function getFileExt()

	{

		return 'xml';

	}

	

	/**

	 * Adds the aLinks settings to the exported XML

	 *

	 * @param DOMElement $settings The Settings element

	 */

	protected function _exportSettings(DOMElement $settings)

	{

		$alinks_settings = Alinks_Settings::get('alinks', null, array());

		foreach($alinks_settings as $key => $value) {

			$setting = $settings->appendChild(new DOMElement('Setting', htmlspecialchars($value)));

			$setting->appendChild(new DOMAttr('key', $key));

			$setting->appendChild(new DOMAttr('factory', 'alinks'));

		}

	}

	

	/**

	 * Exports the factory settings to the exported XML

	 *

	 * @param DOMElement $settings The Settings element

	 */

	protected function _exportFactorySettings(DOMElement $settings)

	{

		$factory_settings = Alinks_Settings::get();

		if (isset($factory_settings['alinks'])) {

			unset($factory_settings['alinks']);

		}



		foreach($factory_settings as $factory => $factory_settings) {

			foreach($factory_settings as $key => $value) {

				$setting = $settings->appendChild(new DOMElement('Setting', htmlspecialchars($value)));

				$setting->appendChild(new DOMAttr('key', $key));

				$setting->appendChild(new DOMAttr('factory', $factory));

			}

		}

	}

	

	/**

	 * Exports the keyphrases to the exported XML

	 *

	 * @param DOMElement $root The root XML element

	 */

	protected function _exportKeyphrases(DOMElement $root)

	{

		$node = $root->appendChild(new DOMElement('Keyphrases'));

		

		if (empty($this->_options[self::EXPORT_HASHES])) {

			$keyphrases = Alinks_Registry::get('Alinks_Keyphrase_Keeper')->fetchAll();

		} else {

			$keyphrases = Alinks_Registry::get('Alinks_Keyphrase_Keeper')->fetchByHashes($this->_options[self::EXPORT_HASHES], true);

		}

		

		foreach($keyphrases as $keyphrase) {

			$k = $node->appendChild(new DOMElement('Keyphrase'));

			$k->appendChild(new DOMAttr('hash', $keyphrase->getHash()));

			$k->appendChild(new DOMAttr('phrase', $keyphrase->getPhrase()));

			$k->appendChild(new DOMAttr('description', $keyphrase->getDescription()));

			$k->appendChild(new DOMAttr('created', $keyphrase->getCreated()));

			$k->appendChild(new DOMAttr('factory', $keyphrase->getFactory()));

			foreach($keyphrase->getOptions() as $key => $value) {

				$option = $k->appendChild(new DOMElement('Option', $value));

				$option->appendChild(new DOMAttr('key', $key));

			}

		}

	}

}

